#pragma once
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <strings.h>
#include <unistd.h>
#include <string.h>

namespace Net_work
{

    const static int default_sockfd = -1;
    const static int backlog = 5;

    enum
    {
        SocketError = 1,
        BindError,
        listenError,
    };

    class Socket
    {

    public:
        virtual ~Socket() {}
        virtual void CreateSocket() = 0;                 // 创建套接字
        virtual void BindSocketOrDie(uint16_t port) = 0; // 绑定套接字
        virtual void ListenSocketOrDie(int backlog) = 0;
        virtual Socket *AcceptConnection(std::string *peerip, uint16_t *peerprot) = 0;
        virtual bool ConnectServer(std::string &serverip, uint16_t serverport) = 0;
        virtual int GetSockFd() = 0;
        virtual void SetSockFd(int socket) = 0;
        virtual void CloseSocket() = 0;
        virtual bool Recv(std::string *out, int size) = 0; // 读取内容;size 单次读取的大小
        virtual bool Send(std::string &in) = 0;

        // TODD

        void BuildListenSocket(uint16_t port, int backlog)
        {
            CreateSocket();
            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }
        bool BuildConnectSocket(std::string &serverip, uint16_t serverport)
        {
            CreateSocket();
            // 不需要手动绑定
            return ConnectServer(serverip, serverport);
        }

        void BuildNormalSocket(int sockfd)
        {
            SetSockFd(sockfd);
        }
    };

    // 如果当前类含有纯虚函数那么这个类即使没有virtual修饰也是一个虚类
    class TcpSocket : public Socket
    {
        int _sockfd;

    public:
        TcpSocket(int sockfd = default_sockfd) : _sockfd(sockfd)
        {
        }
        ~TcpSocket() {}

        // 创建套接字
        void CreateSocket() override
        {
            // 创建套接字        IPv4协议  面向字节流
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
                exit(SocketError);
        }

        // 绑定套接字
        void BindSocketOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            bzero(&local, sizeof local);
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY; // 服务端设置为任何ip地址

            int n = ::bind(_sockfd, (const struct sockaddr *)&local, sizeof local);
            if (n < 0)
            {
                exit(BindError);
            }
        }

        void ListenSocketOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
                exit(listenError);
        }

        Socket *AcceptConnection(std::string *peerip, uint16_t *peerport) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof peer;
            int newsockfd = ::accept(_sockfd, (struct sockaddr *)&peer, &len);

            if (newsockfd < 0)
            {
                return nullptr;
            }

            *peerport = ntohs(peer.sin_port);
            *peerip = inet_ntoa(peer.sin_addr);

            return new TcpSocket(newsockfd);
        }

        bool ConnectServer(std::string &serverip, uint16_t serverport) override
        {
            struct sockaddr_in server;
            bzero(&server, sizeof server);
            server.sin_family = AF_INET;
            // server.sin_addr.s_addr = inet_addr(serverip.c_str());

            // 设置ip地址
            inet_pton(AF_INET, serverip.c_str(), &server.sin_addr);

            server.sin_port = htons(serverport);

            int n = ::connect(_sockfd, (const struct sockaddr *)&server, sizeof server); 
            if (n == 0)
                return true;
            return false;
        }

        int GetSockFd() override
        {
            return _sockfd;
        }

        void SetSockFd(int socket) override
        {
            _sockfd = socket;
        }

        // TODD
        void BuildListenSocket(uint16_t port, int backlog) 
        {
            CreateSocket();

            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }
        bool BuildConnectSocket(std::string &serverip, uint16_t serverport) 
        {
            CreateSocket();
            return ConnectServer(serverip, serverport);
        }

        void BuildNormalSocket(int sockfd) 
        {
            SetSockFd(sockfd);
        }

        void CloseSocket() override
        {
            if (_sockfd != default_sockfd)
                close(_sockfd);
        }

        bool Recv(std::string *out, int size) override
        {
            char inbuffer[size];
            ssize_t n = recv(_sockfd, inbuffer, size - 1, 0);
            if (n > 0)
            {
                inbuffer[n] = 0; // 结尾设置为 \0
                *out += inbuffer;
                return 1;
            }
            return 0;
        }

        bool Send(std::string &in) override
        {
            int n = send(_sockfd, in.c_str(), in.size(), 0);
            if(n<0)
            return 0;
            return 1;
        }
    };

}